<?php
	class Database
	{
		private $type = false;
		private $link;

		public function __toString()
		{
			return $this->type;
		}
		
		public function __construct( $conn = array() )
		{		
			$this->type = $conn['type'];
			switch($this->type) 
			{
				case 'mysql':
					$this->link = mysql_connect($conn['addr'], $conn['user'], $conn['pass']) or die('Could not connect: ' . mysql_error());
					mysql_select_db($conn['name'], $this->link);
					break;

				case 'mysqli':
					$this->link = new mysqli($conn['addr'], $conn['user'], $conn['pass'], $conn['name'], $conn['port'], $conn['socket']); 
					if( $this->link->connect_error ) { die( 'Could not connect to mysqli. Reason: ' . mysqli_connect_error() ); }
					break;
					
				case 'mssql':
					$this->link = mssql_connect($conn['addr'], $conn['user'], $conn['pass']) or die('Could not connect to MSSQL Server '.$conn['addr']);
					mssql_select_db($conn['name'], $this->link);
					break;

				case 'pgsql':
					$this->link = pg_connect('host='.$conn['addr'].' port='.$conn['port'].' dbname='.$conn['name'].' user='.$conn['user'].' password='.$conn['pass']) or die('Could not connect to Postgres');
					break;
					
				default:
					break;
			}
		}
		
		public function disconnect()
		{
			switch($this->type) 
			{
				case 'mysql':
					mysql_close($this->link);
					break;

				case 'mysqli':
					$this->link->close();
					break;
					
				case 'mssql':
					mssql_close($this->link);
					break;

				case 'pgsql':
					pg_close($this->link);
					break;
					
				default:
					break;
			}
		}
				
		public function select($sql, $flatten = false)
		{
			$temp = null;

			switch($this->type) 
			{
				case 'mysql':
					if($results = mysql_query($sql, $this->link))
					{
						while ($row = mysql_fetch_assoc($results)) 
						{
							$temp[] = $row;
						}
					}
					break;

				case 'mysqli':
					if($results = $this->link->query($sql))
					{
						while ($row = $results->fetch_assoc()) 
						{
							$temp[] = $row;
						}
					}
					break;
					
				case 'mssql':
					if($results = mssql_query($sql, $this->link))
					{
						while ($row = mssql_fetch_assoc($results)) 
						{
							$temp[] = $row;
						}
					}
					break;

				case 'pgsql':
					if($results = pg_query($this->link, $sql))
					{
						while ($row = pg_fetch_assoc($results)) 
						{
							$temp[] = $row;
						}
					}
					break;
					
				default:
					break;
			}
			
			if($flatten && count($temp) != 0) { $temp = $temp[0]; }
			
			return $temp;	
		}
	
		public function update($sql)
		{
			return $this->update_insert_delete($sql, 'update');
		}
	
		public function insert($sql)
		{
			return $this->update_insert_delete($sql, 'insert');
		}
	
		public function delete($sql)
		{
			return $this->update_insert_delete($sql, 'delete');
		}

		public function currentval($sequence)
		{
			switch($this->type) 
			{
				case 'mysql':
					$sql = "select value from sequences where name = '$sequence'";
					$temp = $this->select($sql, true);
					$value = $temp['value'];
					break;

				case 'mysqli':
					$sql = "select value from sequences where name = '$sequence'";
					$temp = $this->select($sql, true);
					$value = $temp['value'];
					break;
					
				case 'mssql':
					$sql = "select value from sequences where name = '$sequence'";
					$temp = $this->select($sql, true);
					$value = $temp['value'];
					break;

				case 'pgsql':
					$sql = "select currval('$sequence') as value;";
					$temp = $this->select($sql, true);
					if( !$temp ) { $value = 0; }
					else { $value = $temp['value']; }
					break;
					
				default:
					break;
			}
			return $value;
		}

		public function nextval($sequence)
		{
			switch($this->type) 
			{
				case 'mysql':
					$sql = "update sequences set value = value + step where name = '$sequence'";
					$temp = $this->update($sql);	
					$value = $this->currentval($sequence);
					break;

				case 'mysqli':
					$sql = "update sequences set value = value + step where name = '$sequence'";
					$temp = $this->update($sql);	
					$value = $this->currentval($sequence);
					break;
					
				case 'mssql':
					$sql = "update sequences set value = value + step where name = '$sequence'";
					$temp = $this->update($sql);	
					$value = $this->currentval($sequence);
					break;

				case 'pgsql':
					$sql = "select nextval('$sequence') as value;";
					$temp = $this->select($sql, true);	
					$value = $temp['value'];
					break;
					
				default:
					break;
			}
			return $value;
		}

		private function update_insert_delete($sql, $event_type)
		{
			$success = false;
	
			switch($this->type) 
			{
				case 'mysql':
					if($results = mysql_query($sql, $this->link)) { $success = true; }
					break;

				case 'mysqli':
					if($results = $this->link->query($sql)) { $success = true; }
					break;
					
				case 'mssql':
					if($results = mssql_query($sql, $this->link)) { $success = true; }
					break;

				case 'pgsql':
					if($results = pg_query($this->link, $sql)) { $success = true; }
					break;
					
				default:
					break;
			}
			
			return $success;	
		
		}		
	}
?>
